package com.hushui.mall.member.web;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hushui.mall.member.dao.MemberDao;
import com.hushui.mall.member.entity.MemberEntity;
import com.hushui.mall.member.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Map;

@Controller
public class IndexController {

    @Autowired
    private MemberDao memberDao;


    @RequestMapping("index")
    public String index(){
        return "login";
    }

    @RequestMapping("regist")
    public String regist(){
        return "regist";
    }


    /*登录，返回token*/
    @RequestMapping("login")
    public void login(@RequestBody Map<String,Object> map, HttpServletRequest request, HttpServletResponse response){

        System.out.println("查看是否进入");
        System.out.println(map);

        QueryWrapper<MemberEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username",map.get("username"));
        queryWrapper.eq("password",map.get("password"));

        MemberEntity memberEntity = memberDao.selectOne(queryWrapper);

        if (memberEntity!=null){
            String token = JwtUtil.create(memberEntity.getUsername());
            System.out.println(token);
            Cookie cookie=new Cookie("token",token);
            response.addCookie(cookie);
        }else {
            try {
                PrintWriter writer = response.getWriter();
                writer.write("AAAAA");
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        System.out.println("结束");
    }



}



